/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.service;

import com.stip.server.da.ActDA;
import com.stip.server.da.UserActDA;
import com.stip.server.da.UserMainDA;
import com.stip.server.da.UserTokenDA;
import com.stip.server.entities.Activeties;
import com.stip.server.entities.Admin;
import com.stip.server.entities.UserAct;
import com.stip.server.entities.UsersMain;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
@Path("activity")
public class ActivityService {

    // Add business logic below. (Right-click in editor and choose
    
    // "Insert Code > Add Business Method")
    @EJB
    ActDA actDA;
    @EJB
    UserMainDA userMainDA;
    @EJB
    UserTokenDA userTokenDA;
    @EJB
    UserActDA userActDA;
    
   @GET
   @Path("/{id}")
   @Produces(MediaType.APPLICATION_JSON)
   public Activeties getActivety(@PathParam("id") Integer id){
       if (actDA.findActivetiesByID(id)!=null) {
           return  actDA.findActivetiesByID(id);
       } else {
           throw  new WebApplicationException(404);
       }
   }
   
   @POST
   @Produces(MediaType.APPLICATION_JSON)
   public Activeties postActivety(@QueryParam("forg")Integer firstOrg,@QueryParam("sorg")Integer secondOrg,@QueryParam("torh")Integer thirdOrg,@QueryParam("fe")Integer firstEnsure,@QueryParam("se")Integer secondEnsure,@QueryParam("te")Integer thirdEnsure,@QueryParam("val")Date validity,@QueryParam("loc")Integer location,@QueryParam("abs")String abstracts,@QueryParam("actdate")Date actDate,@QueryParam("url") String url,@QueryParam("email") String email,@QueryParam("status") Integer status ){
   Integer actID= actDA.createActivety(firstOrg, secondOrg, thirdOrg, firstEnsure, secondEnsure, thirdEnsure, validity, location, abstracts, actDate,url,email,status);
   return  actDA.findActivetiesByID(actID);
   }
   @PUT
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/{id}")
   public Boolean putActivety(@QueryParam("token") String token,@PathParam("id") Integer id,@QueryParam("forg")Integer firstOrg,@QueryParam("sorg")Integer secondOrg,@QueryParam("torh")Integer thirdOrg,@QueryParam("fe")Integer firstEnsure,@QueryParam("se")Integer secondEnsure,@QueryParam("te")Integer thirdEnsure,@QueryParam("val")Date validity,@QueryParam("loc")Integer location,@QueryParam("abs")String abstracts,@QueryParam("actdate")Date actDate,@QueryParam("url") String url,@QueryParam("email") String email,@QueryParam("status") Integer status ){
      //查是否存在此活动
       if (actDA.findActivetiesByID(id)!=null) {
//           Activeties activeties=actDA.findActivetiesByID(id);
           //查权限
           if (userTokenDA.findToken(token)!=null) {
               String checkID=userTokenDA.findToken(token);
               //获取权限用户
               UsersMain usersMain=userMainDA.findUsersMainById(checkID);
               if (usersMain!=null) {
                   //获取权限用户所有的组织管理权
                   List<Admin> admins=usersMain.getAdminList();
                   for (Iterator<Admin> it = admins.iterator(); it.hasNext();) {
                       Admin admin = it.next();
                       //遍历看是否对此活动有管理权
                       if (admin.getOrgId().getId()==firstOrg||admin.getOrgId().getId()==secondOrg||admin.getOrgId().getId()==thirdEnsure) {
                           Boolean b= actDA.editActivety(id, firstOrg, secondOrg, thirdOrg, firstEnsure, secondEnsure, thirdEnsure, validity, location, abstracts, actDate,url,email,status);
                           if (b) {
                               return true;
                           } else {
                               throw  new WebApplicationException(400);
                           }
                       }
                       
                   }
                   throw  new WebApplicationException(401);
               } else {
                   throw  new WebApplicationException(400);
               }
           } else {
               throw  new WebApplicationException(401);
           }
       } else {
           throw  new WebApplicationException(404);
       }
   }
   @DELETE
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/{id}")
   public Boolean deleteActivety(@QueryParam("token") String token,@PathParam("id") Integer id){
       if (actDA.findActivetiesByID(id)!=null) {
           Activeties activeties=actDA.findActivetiesByID(id);
           if (userTokenDA.findToken(token)!=null) {
               String checkID=userTokenDA.findToken(token);
               UsersMain usersMain=userMainDA.findUsersMainById(checkID);
               if (usersMain!=null) {
                   List<Admin> admins=usersMain.getAdminList();
                   for (Iterator<Admin> it = admins.iterator(); it.hasNext();) {
                       Admin admin = it.next();
                       if (admin.getOrgId().getId()==activeties.getFirstOrg().getId()||admin.getOrgId().getId()==activeties.getSecondOrg().getId()||admin.getOrgId().getId()==activeties.getThirdOrg().getId()) {
                           Boolean b= actDA.deleteActivety(id);
                           if (b) {
                               return true;
                           } else {
                               throw  new WebApplicationException(400);
                           }
                       }
                       
                   }
                   throw  new WebApplicationException(401);
               } else {
                   throw  new WebApplicationException(400);
               }
           } else {
               throw  new WebApplicationException(401);
           }
       } else {
           throw  new WebApplicationException(404);
       }
   }
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/{id}/user/{uid}")
   public UserAct getUserAct(@PathParam("id") Integer id,@PathParam("uid") String uid,@QueryParam("token") String token){
   if (userActDA.findActByActUid(uid, id)!=null) {
           String checkID=userTokenDA.findToken(token);
           if (checkID!=null&&(checkID == null ? uid == null : checkID.equals(uid))) {
           UserAct userAct =userActDA.findActByActUid(uid, id);
           
               if (userAct!=null) {
                   return  userAct;
               } else {
                   throw  new WebApplicationException(404);
               }
           } else {
               throw  new WebApplicationException(401);
           }
           
       } else {
           throw  new WebApplicationException(404);
       }
   
   
   }
   
   
   
   
   
   
   //status 1;报名 2:签到 3:放弃
   @POST
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/{id}/user")
   public  UserAct postUserAct(@PathParam("id") Integer id,@QueryParam("token") String token){
       if (actDA.findActivetiesByID(id)!=null) {
          
           if (userTokenDA.findToken(token)!=null) {
               String checkID=userTokenDA.findToken(token);
              return  userActDA.findActByID(userActDA.createAct(id, checkID, 0)) ;
               
           } else {
               throw  new WebApplicationException(401);
           }
       } else {
           throw  new WebApplicationException(404);
       }
   }
   @PUT
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/{id}/user/{uid}")
   public UserAct putUserAct(@PathParam("id") Integer id,@PathParam("uid") String uid,@QueryParam("token") String token,@QueryParam("status") Integer status){
       if (userActDA.findActByActUid(uid, id)!=null) {
           String checkID=userTokenDA.findToken(token);
           if (checkID!=null&&(checkID == null ? uid == null : checkID.equals(uid))) {
           Integer userActID=    userActDA.findActByActUid(uid, id).getId();
            Boolean flag=    userActDA.editUAct(userActID, id, uid, status);
               if (flag) {
                   return  userActDA.findActByID(userActID);
               } else {
                   throw  new WebApplicationException(400);
               }
           } else {
               throw  new WebApplicationException(401);
           }
           
       } else {
           throw  new WebApplicationException(404);
       }
   }
   
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/{id}/users")
   public List<UserAct> getUserActs(@PathParam("id") Integer id){
       Activeties activeties=actDA.findActivetiesByID(id);
       if (activeties!=null) {
           return   activeties.getUserActList();
       } else {
           throw new WebApplicationException(404);
       }
      
   }
}
